package com.ms.qw.infraestructura.persistence.jpa.sigar;

import com.ms.qw.client.dto.*;
import com.ms.qw.domain.model.Racion;
import com.ms.qw.domain.model.RacionRepository;
import com.ms.qw.domain.model.Receta;
import com.ms.qw.domain.model.RecetaRepository;
import com.ms.qw.server.infrastructure.mapper.MSResultTransformer;
import com.ms.support.jpa.BaseDAO;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Repository
public class RacionRepositoryImpl extends BaseDAO<Racion, Integer> implements RacionRepository {

    private final static Logger logger = LoggerFactory.getLogger(RacionRepositoryImpl.class);


    public List<RacionDetalleDTO> findDetalle(Integer codigo) {

        StringBuilder sql=new StringBuilder();
        sql.append("SELECT PDR.iCodDetRacion     as \"codigo\", ");
        sql.append("       PMR.iCodReceta     as \"codigoReceta\", ");
        sql.append("       PMR.iCodReceta     as \"receta.codigo\", ");
        sql.append("       PMR.vCodRecetaQW   as \"receta.codigoQW\", ");
        sql.append("       PMR.cCodRegion     as \"receta.regionAlimentaria\", ");
        sql.append("       PMR.iCodTipoReceta as \"receta.codigoTipoReceta\", ");
        sql.append("       PMR.iCodCatReceta  as \"receta.codigoCategoriaReceta\", ");
        sql.append("       PMR.iCodClaseReceta as \"receta.codigoClaseReceta\",  ");
        sql.append("       PMR.vNombreReceta  as \"receta.nombre\", ");
        sql.append("       PMR.vAlias         as \"receta.alias\", ");
        sql.append("       PDR.dFecCreacion   as \"receta.fechaCreacion\", ");
        sql.append("       PDR.cCreadoPor     as \"receta.creadoPor\" ");
        sql.append("FROM   [EQSIGEPCO].[PRDETALLERACION] PDR INNER JOIN ");
        sql.append("       [EQSIGEPSA].[PRMAERECETA] PMR ON PMR.iCodReceta= PDR.iCodReceta ");
        sql.append("WHERE  PDR.iCodRacion= "+codigo);


        org.hibernate.Query query = em().unwrap(Session.class)
                .createSQLQuery(sql.toString())
                .setResultTransformer(new MSResultTransformer(RacionDetalleDTO.class));
        return query.list();
    }

    public RacionDTO findForEdit(Integer codigo) {

        StringBuilder sql = new StringBuilder();
        sql.append("SELECT iCodRacion     as \"codigo\", ");
        sql.append("       cCodRacionQW   as \"codigoQW\", ");
        sql.append("       cCodRegion     as \"regionAlimentaria\", ");
        sql.append("       iCodTipoRacion as \"codigoTipoRacion\", ");
        sql.append("       iCodClaseRacion as \"codigoClaseRacion\",  ");
        sql.append("       vRacion           as \"nombre\", ");
        sql.append("       vAliasQW         as \"alias\", ");
        sql.append("       dFecCreacion   as \"fechaCreacion\", ");
        sql.append("       iOrden         as \"orden\", ");
        sql.append("       cEstado        as \"estado\", ");
        sql.append("       cCreadoPor     as \"creadoPor\" ");
        sql.append("FROM   [EQSIGEPSA].[PRTABRACION] ");
        sql.append("WHERE  iCodRacion= "+codigo);


        org.hibernate.Query query = em().unwrap(Session.class)
                .createSQLQuery(sql.toString())
                .setResultTransformer(new MSResultTransformer(RacionDTO.class));
        return (RacionDTO)query.uniqueResult();
    }



}
